﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using Microsoft.Office;
using Microsoft.Vbe.Interop;
//using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace LcAudit.Common
{
    public class ExWord
    {
        public static bool ExportToWordExped(string ftxt, string filepath)
        {
            bool isfind = false;
            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            try
            {
                GC.Collect();
                object missing = System.Reflection.Missing.Value;
                object Nothing = System.Reflection.Missing.Value;


                //创建一个名为WordDoc的文档对象               
                Microsoft.Office.Interop.Word.Document doc;
                object b = filepath;
                doc = WordApp.Documents.Open(ref b, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                isfind = find(ftxt, ftxt, Nothing, WordApp);
                doc.Close(ref Nothing, ref Nothing, ref Nothing);
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                WordApp = null;
                doc = null;
                KillWord();
                GC.Collect();

            }
            catch (Exception ex)
            {
                KillWord();
                throw ex;
            }
            return isfind;
        }
        public static string ExportFile(string gudong, string cldate, string agent, string manager,
            string add, string filen, string filet, string bdresult, string bider, string bh, string priceCompare)
        {
            object outname;
            try
            {
                GC.Collect();
                object missing = System.Reflection.Missing.Value;
                object Nothing = System.Reflection.Missing.Value;
                //创建WordApp对象
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();


                //创建一个名为WordDoc的文档对象
                //string name = ;
                Microsoft.Office.Interop.Word.Document doc;
                object b = System.Web.HttpContext.Current.Server.MapPath("~/template/p.doc");

                doc = WordApp.Documents.Open(ref b, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                findlong("@bider@", bider, Nothing, WordApp);
                findlong("@gudong@", gudong, Nothing, WordApp);
                findlong("@cldate@", cldate, Nothing, WordApp);
                findlong("@agent@", agent, Nothing, WordApp);
                findlong("@manager@", manager, Nothing, WordApp);
                findlong("@add@", add, Nothing, WordApp);
                findlong("@filen@", filen, Nothing, WordApp);
                findlong("@filet@", filet, Nothing, WordApp);
                findlong("@bdresult@", bdresult, Nothing, WordApp);
                TypeText("@priceCompare@", priceCompare, Nothing, WordApp);
                //find("@gudong@", gudong, Nothing, WordApp);

                outname = System.Web.HttpContext.Current.Server.MapPath("~/out/") + bh + ".doc";

                if (File.Exists(outname.ToString()))
                    File.Delete(outname.ToString());
                doc.SaveAs(ref outname, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                doc.Close(ref Nothing, ref Nothing, ref Nothing);
                outname = "/out/" + bh + ".doc";
                //关闭WordApp对象
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                WordApp = null;
                doc = null;
                KillWord();
                GC.Collect();

            }
            catch (Exception ex)
            {
                KillWord();
                throw ex;
            }
            return outname.ToString();
        }
        public static string ExportTops(System.Data.DataSet ds)
        {
            object outname;
            try
            {
                GC.Collect();
                object missing = System.Reflection.Missing.Value;
                object Nothing = System.Reflection.Missing.Value;
                //创建WordApp对象
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();


                //创建一个名为WordDoc的文档对象
                //string name = ;
                Microsoft.Office.Interop.Word.Document doc;
                object b = System.Web.HttpContext.Current.Server.MapPath("~/template/p.doc");

                doc = WordApp.Documents.Open(ref b, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                string unit = ds.Tables[0].Rows[0]["sname"].ToString();
                find("@name@", unit, Nothing, WordApp);
                object beforeRow = doc.Content.Tables[1].Rows[2];
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    doc.Content.Tables[1].Rows.Add(ref beforeRow);
                    doc.Content.Tables[1].Cell(i + 2, 1).Range.Text = ds.Tables[0].Rows[i]["proid"].ToString();
                    doc.Content.Tables[1].Cell(i + 2, 2).Range.Text = ds.Tables[0].Rows[i]["projectName"].ToString();
                    doc.Content.Tables[1].Cell(i + 2, 3).Range.Text = ds.Tables[0].Rows[i]["material"].ToString();
                    doc.Content.Tables[1].Cell(i + 2, 4).Range.Text = ds.Tables[0].Rows[i]["thisMonthCount"].ToString();
                    doc.Content.Tables[1].Cell(i + 2, 8).Range.Text = ds.Tables[0].Rows[i]["arrivalAddress"].ToString();
                    doc.Content.Tables[1].Cell(i + 2, 9).Range.Text = ds.Tables[0].Rows[i]["linkman"].ToString();
                    doc.Content.Tables[1].Cell(i + 2, 10).Range.Text = ds.Tables[0].Rows[i]["contactWay"].ToString();
                }
                outname = System.Web.HttpContext.Current.Server.MapPath("~/out/") + DateTime.Now.Ticks.ToString() + "p.doc";

                if (File.Exists(outname.ToString()))
                    File.Delete(outname.ToString());
                doc.SaveAs(ref outname, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                doc.Close(ref Nothing, ref Nothing, ref Nothing);

                //关闭WordApp对象
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                WordApp = null;
                doc = null;
                KillWord();
                GC.Collect();



            }
            catch (Exception ex)
            {
                KillWord();
                throw ex;
            }
            return outname.ToString();
        }

        private static bool find(object str, object replacestr, object Nothing, Microsoft.Office.Interop.Word.Application WordApp)
        {
            WordApp.Selection.WholeStory();

            Find find = WordApp.Selection.Find; ;
            find.ClearFormatting();

            //object str = "school";
            //object replacestr = ds.Tables[0].Rows[0]["school"].ToString();
            return find.Execute(ref str, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref replacestr, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);


        }

        private static bool findlong(object str, object replacestr, object Nothing, Microsoft.Office.Interop.Word.Application WordApp)
        {
            WordApp.Options.ReplaceSelection = true;
            WordApp.Selection.Find.ClearFormatting();
            WordApp.Selection.Find.Text = str.ToString();
            WordApp.Selection.Find.Replacement.Text = "";
            WordApp.Selection.Find.Forward = true;
            WordApp.Selection.Find.Wrap = WdFindWrap.wdFindContinue;
            WordApp.Selection.Find.Format = false;
            WordApp.Selection.Find.MatchCase = false;
            WordApp.Selection.Find.MatchWholeWord = false;
            WordApp.Selection.Find.MatchByte = true;
            WordApp.Selection.Find.MatchWildcards = false;
            WordApp.Selection.Find.MatchSoundsLike = false;
            WordApp.Selection.Find.MatchAllWordForms = false;
            bool isr= WordApp.Selection.Find.Execute(ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            WordApp.Selection.TypeText(replacestr.ToString());
            return isr;

        }

        private static bool TypeText(object str, object replacestr, object Nothing, Microsoft.Office.Interop.Word.Application WordApp)
        {
            WordApp.Selection.WholeStory();

            Find find = WordApp.Selection.Find;
            find.ClearFormatting();
            find.Text = str.ToString();
            find.Execute();
            WordApp.Selection.TypeText(replacestr.ToString());

            return true;
            //object str = "school";
            //object replacestr = ds.Tables[0].Rows[0]["school"].ToString();
            //return find.Execute(ref str, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
            //    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);


        }


        private static void KillWord()
        {
            foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("WINWORD"))
            {
                p.Kill();
            }
        }
    }
}
